package com.zwk.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.ram.model.v20150501.DeleteRoleRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.zwk.base.handler.GuLiException;
import com.zwk.vod.service.VodService;
import com.zwk.vod.util.InitVodClient;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

/**
 * @author zwk
 * @create 2022/8/24 16:33
 */
@Service
public class VodServiceImpl implements VodService {

    @Value("${aliyun.oss.file.keyid}")
    private String keyId;

    @Value("${aliyun.oss.file.keysecret}")
    private String keySecret;

    // 上传视频到阿里云
    @Override
    public String uploadAliyun(MultipartFile file) {

        UploadStreamRequest request = null;
        try {
            // 有唯一的id值，重名也没有问题
            String fileName = file.getOriginalFilename();
            String title = fileName;
            request = new UploadStreamRequest(keyId, keySecret, title, fileName, file.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }

        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);

        String videoId = null;

        if (response.isSuccess()) {
            videoId = response.getVideoId();
        } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
            videoId = response.getVideoId();
            System.out.print("ErrorCode=" + response.getCode() + "\n");
            System.out.print("ErrorMessage=" + response.getMessage() + "\n");
        }
            return videoId;
    }

    @Override
    public boolean removeVideo(String id) {

        // 初始化对象
        DefaultAcsClient client = InitVodClient.initVodClient(keyId, keySecret);
        // 创建删除视频的request对象
        DeleteVideoRequest request = new DeleteVideoRequest();
        // 设置视频id
        request.setVideoIds(id);
        try {
            client.getAcsResponse(request);
            return true;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }

    }

    @Override
    public boolean removeMoreVideo(List<String> videoList) {
        // 初始化对象
        DefaultAcsClient client = InitVodClient.initVodClient(keyId, keySecret);
        // 创建删除视频的request对象
        DeleteVideoRequest request = new DeleteVideoRequest();

        // videoList中的值转换成1,2,3
        String ids = StringUtils.join(videoList.toArray(), ",");
        // 设置视频id
        request.setVideoIds(ids);
        try {
            client.getAcsResponse(request);
            return true;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        }
    }
}
